package com.pipi.biyesheji.service.impl;

import com.pipi.biyesheji.dao.manager.ProductManager;
import com.pipi.biyesheji.dao.manager.UserManager;
import com.pipi.biyesheji.dao.model.ProductDO;
import com.pipi.biyesheji.dao.model.UserDO;
import com.pipi.biyesheji.dao.query.UserQuery;
import com.pipi.biyesheji.enums.BooleanEnum;
import com.pipi.biyesheji.pojo.User;
import com.pipi.biyesheji.service.UserRoleService;
import com.pipi.biyesheji.service.UserService;
import com.pipi.biyesheji.util.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author pipi
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserManager userManager;

    @Autowired
    private ProductManager productManager;

    @Override
    public String getPassword(String name) {
        User u = getByName(name);
        if (Objects.isNull(u)) {
            return null;
        }
        return u.getPassword();
    }

    @Override
    public User getByName(String name) {
        UserQuery userQuery = new UserQuery();
        userQuery.createCriteria().andNameEqualTo(name);
        List<UserDO> userDOS = userManager.selectByQuery(userQuery);
        if (CollectionUtils.isEmpty(userDOS)) {
            return null;
        }
        return buildUser(userDOS.get(0));

    }

    @Override
    public List<User> list() {
        UserQuery userQuery = new UserQuery();
        List<UserDO> userDOS = userManager.selectByQuery(userQuery);
        return buildUsers(userDOS);
    }

    @Override
    public void add(User user) {
        UserDO userDO = BeanUtils.createCloneBean(user, UserDO.class);
        userManager.insertSelective(userDO);
    }

    @Override
    public void delete(Long id) {
        userManager.deleteByPrimaryKey(id);
        userRoleService.deleteByUser(id);
    }

    @Override
    public User get(Long id) {
        UserDO userDO = userManager.selectByPrimaryKey(id);
        return buildUser(userDO);
    }

    @Override
    public void update(User user) {
        UserDO userDO = BeanUtils.createCloneBean(user, UserDO.class);
        userManager.updateByPrimaryKeySelective(userDO);
    }

    @Override
    public String updateStatus(String name, Integer status) {
        UserQuery userQuery = new UserQuery();
        userQuery.createCriteria().andNameEqualTo(name);

        UserDO userDO = new UserDO();
        userDO.setStatus(BooleanEnum.YES.getCode());
        userManager.updateByQuerySelective(userDO, userQuery);
        return "success";
    }

    @Override
    public User getUserByPid(Long pid) {
        ProductDO productDO = productManager.selectByPrimaryKey(pid);
        UserDO userDO = userManager.selectByPrimaryKey(productDO.getBid());
        return buildUser(userDO);
    }


    private List<User> buildUsers(List<UserDO> userDOList) {
        return userDOList.stream().map(this::buildUser).collect(Collectors.toList());
    }

    private User buildUser(UserDO userDO) {
        if (Objects.isNull(userDO)) {
            return null;
        }
        return BeanUtils.createCloneBean(userDO, User.class);
    }

}